/**
 * 
 */
package com.gr.staffpm.notes.activity.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gr.staffpm.dao.HibernateDao;
import com.gr.staffpm.datatypes.Activities;
import com.gr.staffpm.datatypes.Activity;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.User;


/**
 * @author Graham Rhodes 21 Feb 2011 00:23:55
 */
@SuppressWarnings("unchecked")
@Repository("activityDAO")
public class HibernateActivityDAO extends HibernateDao implements ActivityDAO {

    private static final long serialVersionUID = 1L;

    /* (non-Javadoc)
     * @see com.gr.staffpm.notes.activity.dao.ActivityDAO#getRecentActivityForUser(int)
     */
    @Override
    public List<Activity> getRecentActivityForUser(User user, Date start, Date end) {
        Criteria crit = getSession().createCriteria(Activity.class);
        crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        crit.add(Restrictions.eq(Activity.NOTIFIED, user));
        crit.add(Restrictions.between(Activity.CREATED, start, end));
        return crit.list();
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.notes.activity.dao.ActivityDAO#getActivityType()
     */
    @Override
    public Activities getActivityType(String activityType) {
        Criteria crit = getSession().createCriteria(Activities.class);
        crit.add(Restrictions.eq(Activities.VALUE, activityType));
        return (Activities) crit.uniqueResult();
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.notes.activity.dao.ActivityDAO#addActivity(com.gr.staffpm.datatypes.Activities, com.gr.staffpm.datatypes.User, com.gr.staffpm.datatypes.Task, boolean, com.gr.staffpm.datatypes.User, java.util.Date)
     */
    @Override
    public void addActivity(Activities activityType, User notified, Task task, boolean newNote, User creator, Date created) {
        Activity activity = new Activity();
        activity.setActivity(activityType);
        activity.setNotified(notified);
        activity.setTask(task);
        activity.setNewNote(newNote);
        activity.setCreatedBy(creator);
        activity.setCreated(created);
        activity.setUpdatedBy(creator);
        activity.setLastUpdated(created);
        getSession().save(activity);
        getSession().flush();
    }

    @Override
    public void updateActivity(Activity activity) {
        getSession().saveOrUpdate(activity);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.notes.activity.dao.ActivityDAO#getNewActivityCount(com.gr.staffpm.datatypes.User)
     */
    @Override
    public int getNewActivityCount(User user) {
        Criteria crit = getSession().createCriteria(Activity.class);
        crit.add(Restrictions.eq(Activity.NOTIFIED, user));
        crit.add(Restrictions.eq(Activity.NEW_NOTE, true));
        crit.setProjection(Projections.rowCount());
        return ((Long) crit.uniqueResult()).intValue();
    }

}
